//
//  SMNewsViewController.m
//  ShoppingMall
//
//  Created by Demon on 2018/3/21.
//  Copyright © 2018年 Demon. All rights reserved.
//

#import "SMNewsViewController.h"
#import <HTHorizontalSelectionList.h>
#import "SMNewsTableViewCell.h"
#import "SMNewsModel.h"
#import "YDWebViewController.h"
#import "SMNewsDetailViewController.h"

@interface SMNewsViewController () <HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource, UITableViewDataSource, UITableViewDelegate>
{
    NSString *menuURL;
    NSInteger page;
}
@property (weak, nonatomic) IBOutlet UITableView *newsTableView;
@property (nonatomic, strong) NSMutableArray *newsModelArray;
@property (weak, nonatomic) IBOutlet HTHorizontalSelectionList *newsHori;
@property(nonatomic, strong) NSArray *titlesArray;
@property(nonatomic, strong) NSArray *titils;

@end

@implementation SMNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.newsHori.dataSource = self;
    self.newsHori.delegate = self;
    self.newsHori.backgroundColor = UIColorHex(ffffff);
    self.newsHori.showsEdgeFadeEffect = YES;
    [self.newsHori setTitleFont:UIFONTSIZE(15) forState:UIControlStateNormal];
    [self.newsHori setTitleFont:UIFONTSIZE(15) forState:UIControlStateSelected];
    [self.newsHori setTitleColor:UIColorHex(33333) forState:UIControlStateNormal];
    [self.newsHori setTitleColor:UIColorHex(FFAA1A) forState:UIControlStateSelected];
    self.newsHori.bottomTrimColor = UIColorHex(999999);
    self.newsHori.selectionIndicatorColor = UIColorHex(FFAA1A);
    self.newsHori.snapToCenter = YES;
    [self.newsTableView registerNib:[UINib nibWithNibName:@"SMNewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"SMNewsTableViewCell"];
    page = 1;
    [self.newsTableView addHeadFresh:self selector:@selector(headerFresh)];
    MJRefreshBackNormalFooter *refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
    refreshFooter.automaticallyChangeAlpha = YES;
    self.newsTableView.mj_footer = refreshFooter;
    
    [self getNewsData];
}

- (void)headerFresh {
    [self.newsTableView.mj_footer resetNoMoreData];
    page = 1;
    if (self.titlesArray.count <= 0 || menuURL.length <= 0) {
        [self getNewsData];
    } else {
        [self getNewsContent];
    }
}

- (void)footerFresh {
    page ++;
    [self getNewsContent];
}

- (void)getNewsData {
    [[YDNetClient sharedInstance] getMethod:@"http://jidianping.com/api/tt/toutiao.php" param:nil success:^(id object) {
        self.titlesArray = object[@"cate"];
        self.titils = [self.titlesArray valueForKeyPath:@"menuName"];
        [self.newsHori reloadData];
        if (self.titlesArray.count) {
            NSDictionary *firstDic = self.titlesArray[0];
            menuURL = firstDic[@"menuUrl"];
            [self getNewsContent];
        }
    } fail:^(NSError *error) {
        
    }];
}

- (void)getNewsContent {
    NSString *pageNumber = [NSString stringWithFormat:@"%ld",(long)page];
    [[YDNetClient sharedInstance] getMethod:[NSString stringWithFormat:@"%@&page=%@",menuURL,pageNumber] param:nil success:^(id object) {
        [self.newsTableView endFresh];
        if (page == 1) {
            [self.newsModelArray removeAllObjects];
        }
        NSArray *array = [SMNewsModel mj_objectArrayWithKeyValuesArray:object[@"listData"]];
        if (array.count > 0) {
            [self.newsModelArray addObjectsFromArray:array];
        } else {
            if (page > 1) page --;
            [self.newsTableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.newsTableView reloadData];
    } fail:^(NSError *error) {
        if (page > 1) page --;
        [self.newsTableView endFresh];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SMNewsModel *newsModel = self.newsModelArray[indexPath.row];
    if (newsModel.detailUrl && newsModel.detailUrl.length > 5) {
        SMNewsDetailViewController *vc = MainStoryBoardVC(@"SMNewsDetailViewController");
        vc.newsDetailURL = newsModel.detailUrl;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (newsModel.h5url && newsModel.h5url.length > 5) {
        YDWebViewController *webVC = ALLOC(YDWebViewController);
        webVC.webUrl = newsModel.h5url;
        webVC.navigationItem.title = @"资讯详情";
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.newsModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SMNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMNewsTableViewCell"];
    SMNewsModel *newsModel = self.newsModelArray[indexPath.row];
    [cell setNewsCellModel:newsModel];
    return cell;
}

- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.titlesArray.count;
}

- (nullable NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    return self.titils[index];
}

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    if (self.titlesArray.count) {
        NSDictionary *firstDic = self.titlesArray[index];
        page = 1;
        menuURL = firstDic[@"menuUrl"];
        [self getNewsContent];
    }
}

- (NSMutableArray *)newsModelArray {
    if (!_newsModelArray) {
        _newsModelArray = [NSMutableArray array];
    }
    return _newsModelArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
